package com.g3d.objects;

import com.g3d.core.G3DMusicManager;

import android.media.MediaPlayer;

public class G3DMusic 
{

	private final MediaPlayer _player;
	private final G3DMusicManager _manager;
	
	private float _leftVolume = 1.0f;
	private float _rightVolume = 1.0f;
	
	public G3DMusic(MediaPlayer player, G3DMusicManager manager)
	{
		this._player = player;
		this._manager = manager;
	}
	
	public float getLeftVolume()
	{
		return this._leftVolume;
	}
	
	public float getRightVolume()
	{
		return this._rightVolume;
	}
	
	//TODO: Music manipulation calls
	public boolean isPlaying()
	{
		return this._player.isPlaying();
	}
	
	public boolean isLooping()
	{
		return this._player.isLooping();		
	}
	
	public void play()
	{
		this._player.start();
	}
	
	public void stop()
	{
		this._player.stop();
	}
	
	public void pause()
	{
		this._player.pause();
	}
	
	public void resume()
	{
		this._player.start();
	}

	public void setLooping(boolean loop)
	{
		this._player.setLooping(loop);
	}
	
	public void seek(int milliseconds)
	{
		this._player.seekTo(milliseconds);
	}
	
	public int duration()
	{
		return this._player.getDuration();
	}
	
	public void setVolume(float leftVolume, float rightVolume)
	{
		this._leftVolume = leftVolume;
		this._rightVolume = rightVolume;
		
		float actualLeft = this._leftVolume * _manager.masterVolume();
		float actualRight = this._rightVolume * _manager.masterVolume();
		
		this._player.setVolume(actualLeft, actualRight);
	}
	
	public void updateVolume()
	{
		float actualLeft = this._leftVolume * _manager.masterVolume();
		float actualRight = this._rightVolume * _manager.masterVolume();
		
		this._player.setVolume(actualLeft, actualRight);
	}
	
	public void destroy()
	{
		this._player.stop();
		this._player.release();
	}
}
